package com.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置类
 * 配置RedisTemplate的序列化方式
 * 
 * @Configuration 注解标识这是一个配置类
 */
@Configuration
public class RedisConfig {
    
    /**
     * 配置RedisTemplate Bean
     * 设置key和value的序列化方式为StringRedisSerializer
     * 
     * @param factory Redis连接工厂
     * @return 配置好的RedisTemplate实例
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        
        // 设置连接工厂
        redisTemplate.setConnectionFactory(factory);
        
        // key序列化方式
        redisTemplate.setKeySerializer(redisSerializer);
        
        // value序列化方式
        redisTemplate.setValueSerializer(redisSerializer);
        
        // hash key序列化方式
        redisTemplate.setHashKeySerializer(redisSerializer);
        
        // hash value序列化方式
        redisTemplate.setHashValueSerializer(redisSerializer);
        
        return redisTemplate;
    }
}
